package ru.aptu.gsp_banking.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ru.aptu.gsp_banking.service.AccountService;
import ru.aptu.gsp_banking.service.CurrencyService;
import ru.aptu.gsp_banking.service.TransactionService;

import java.security.Principal;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 11/23/11
 * Time: 6:08 PM
 */

@Controller
public class TransactionController {

    @Autowired
    private TransactionService transactionService;

    @Autowired
    private AccountService accountService;

    private Integer accountId;

    @RequestMapping("/transactions/{accountId}")
    public String listTransactions(@PathVariable("accountId") Integer accountId, Map<String, Object> map, Principal principal) {
        if (principal == null) {
            return "redirect:../logout";
        }

        this.accountId = accountId;

        if (accountService.checkAccount(accountId, principal.getName())) {
            map.put("accountNumber", accountService.getAccountNumber(this.accountId));
            map.put("transactionList", transactionService.listTransaction(this.accountId));
            return "transactions";
        }
        return "error403";
    }

}
